package com.pdstudio.charts;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.view.View;

public class LineChartView extends View {

	public int XPoint = 40; // 原点的X坐标
	public int YPoint = 260; // 原点的Y坐标
	public int XScale = 55; // X的刻度长度
	public int YScale = 40; // Y的刻度长度
	public int XLength = 380; // X轴的长度
	public int YLength = 240; // Y轴的长度
	public String[] XLabel; // X的刻度
	public String[] YLabel; // Y的刻度
	public String[] Data; // 数据
	public String[] Data1;// 因为要画两根线，加一个数据
	public String Title; // 显示的标题

	public LineChartView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public LineChartView(Context context, int xpoint, int ypoint, int xscale,
			int yscale, int xlength, int ylength, String[] xlabels,
			String[] ylabels, String[] data, String[] data1, String title) {
		super(context);
		this.XPoint = xpoint;
		this.YPoint = ypoint;
		this.XScale = xscale;
		this.YScale = yscale;
		this.XLength = xlength;
		this.YLength = ylength;
		this.XLabel = xlabels;
		this.YLabel = ylabels;
		this.Data = data;
		this.Data1 = data1;
		this.Title = title;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);// 重写onDraw方法

		// canvas.drawColor(Color.WHITE);//设置背景颜色
		Paint paint = new Paint();
		paint.setStyle(Paint.Style.STROKE);
		paint.setAntiAlias(true);// 去锯齿
		paint.setColor(Color.WHITE);// 颜色
		paint.setStrokeWidth(6);
		Paint paint1 = new Paint();
		paint1.setStyle(Paint.Style.STROKE);
		paint1.setAntiAlias(true);// 去锯齿
		paint1.setColor(Color.DKGRAY);
		paint.setTextSize(24); // 设置轴文字大小
		Paint pc = new Paint();// 画实心圆
		pc.setStyle(Paint.Style.FILL);
		pc.setAntiAlias(true);
		pc.setColor(Color.WHITE);
		Paint pc1 = new Paint();
		pc1.setStyle(Paint.Style.FILL);
		pc1.setAntiAlias(true);
		pc1.setColor(0x7fffffff);

		// 设置Y轴
		/*
		 * 20141020 去掉y轴显示,随时可以把下面一段恢复，就可以显示y轴
		 */
		// canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint); //
		// 轴线
		/*
		 * 20141020去掉y轴刻度跟蚊子,还有箭头，恢复下面注释的即可显示
		 */
		/*
		 * for (int i = 0; i * YScale < YLength; i++) { canvas.drawLine(XPoint,
		 * YPoint - i * YScale, XPoint + 5, YPoint - i YScale, paint); // 刻度 try
		 * { canvas.drawText(YLabel[i], XPoint - 22, YPoint - i * YScale + 5,
		 * paint); // 文字 } catch (Exception e) { } } canvas.drawLine(XPoint,
		 * YPoint - YLength, XPoint - 3, YPoint - YLength + 6, paint); // 箭头
		 * canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint -
		 * YLength + 6, paint);
		 */
		// 设置X轴
		/*
		 * 20141020 去掉x轴显示,随时可以把下面一段恢复，就可以显示x轴
		 */
		// canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint); //
		// 轴线
		for (int i = 0; i * XScale < XLength; i++) {
			/*
			 * 20141020去掉x轴刻度，，恢复下面的注释即可显示
			 */
			/*
			 * canvas.drawLine(XPoint + i * XScale, YPoint, XPoint + i * XScale,
			 * YPoint - 5, paint); // 刻度
			 */
			try {
				/*
				 * 20141020去掉x轴，显示，去掉箭头，恢复下面的注释即可显示
				 */
				/*
				 * canvas.drawText(XLabel[i], XPoint + i * XScale - 10, YPoint +
				 * 20, paint); // 文字
				 */
				int tempi = i;
				// 数据值
				if (i > 0 && YCoord(Data[i - 1]) != -999
						&& YCoord(Data[i]) != -999) // 保证有效数据
					canvas.drawLine(XPoint + (i - 1) * XScale,
							YCoord(Data[i - 1]), XPoint + i * XScale,
							YCoord(Data[i]), paint);

				// 数据值2
				if (tempi > 0 && YCoord(Data1[tempi - 1]) != -999
						&& YCoord(Data1[tempi]) != -999)
					canvas.drawLine(XPoint + (tempi - 1) * XScale,
							YCoord(Data1[tempi - 1]), XPoint + tempi * XScale,
							YCoord(Data1[tempi]), paint);

				// 在圈圈上生成文字
				Paint p = new Paint();
				p.setColor(Color.WHITE);
				p.setTypeface(Typeface.create("宋体", Typeface.BOLD));
				p.setAntiAlias(true);// 去除锯齿
				p.setFilterBitmap(true);// 对位图进行滤波处理
				p.setTextSize(scalaFonts(28));
				// float tX = (XPoint - getFontlength(p, "tiantian"))/2;
				// float tY = (i * XScale-5 -
				// getFontHeight(p))/2+getFontLeading(p);
				float textX = 0;// 文字显示的x轴坐标
				String text = "tiantian";
				try {
					textX = XPoint + i * XScale;
					float tempt = scalaFonts(24);// 这里因为字体是12，只是试试看这么算对不对
					tempt = tempt / 2;
					textX = textX - 20;
				} catch (Exception e) {
					e.printStackTrace();
				}
				canvas.drawText(Data[i] + "℃", textX, YCoord(Data[i]) + 40, p);
				canvas.drawText(Data1[i] + "℃", textX, YCoord(Data1[i]) - 40, p);
				/*
				 * 去掉图片的显示，恢复下面两行可以显示图片
				 */
				// Bitmap
				// b=BitmapFactory.decodeResource(getResources(),R.drawable.biz_plugin_weather_sunny);
				// canvas.drawBitmap(b, XPoint + i *
				// XScale,YCoord(Data[i])+80,p);

			} catch (Exception e) {
			}
		}
		canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6,
				YPoint - 3, paint); // 箭头
		canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6,
				YPoint + 3, paint);
		paint.setTextSize(24);
		/*
		 * 20141020去掉标题的显示，恢复下面一行可以显示标题
		 */
		// canvas.drawText(Title, 150, 50, paint);
		for (int i = 0; i * XScale < XLength; i++) {
			try {
				canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 10, pc1);
				canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 5, pc);
				int tempi = i;
				canvas.drawCircle(XPoint + tempi * XScale,
						YCoord(Data1[tempi]), 10, pc1);
				canvas.drawCircle(XPoint + tempi * XScale,
						YCoord(Data1[tempi]), 5, pc);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private int YCoord(String y0) // 计算绘制时的Y坐标，无数据时返回-999
	{
		int y;
		try {
			y = Integer.parseInt(y0);
		} catch (Exception e) {
			return -999; // 出错则返回-999
		}
		try {
			return YPoint - y * YScale / Integer.parseInt(YLabel[1]);
		} catch (Exception e) {
		}
		return y;
	}

	/*
	 * 默认采用白色字体，宋体文字加粗
	 */
	public static Bitmap getImage(int width, int height, String mString,
			int size) {
		return getImage(width, height, mString, size, Color.RED,
				Typeface.create("宋体", Typeface.BOLD));
	}

	public static Bitmap getImage(int width, int height, String mString,
			int size, int color) {
		return getImage(width, height, mString, size, color,
				Typeface.create("宋体", Typeface.BOLD));
	}

	public static Bitmap getImage(int width, int height, String mString,
			int size, int color, String familyName) {
		return getImage(width, height, mString, size, color,
				Typeface.create(familyName, Typeface.BOLD));
	}

	public static Bitmap getImage(int width, int height, String mString,
			int size, int color, Typeface font) {
		int x = width;
		int y = height;

		Bitmap bmp = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
		// 图象大小要根据文字大小算下,以和文本长度对应
		Canvas canvasTemp = new Canvas(bmp);
		canvasTemp.drawColor(Color.BLACK);
		Paint p = new Paint();
		p.setColor(color);
		p.setTypeface(font);
		p.setAntiAlias(true);// 去除锯齿
		p.setFilterBitmap(true);// 对位图进行滤波处理
		p.setTextSize(scalaFonts(size));
		float tX = (x - getFontlength(p, mString)) / 2;
		float tY = (y - getFontHeight(p)) / 2 + getFontLeading(p);
		canvasTemp.drawText(mString, tX, tY, p);

		return bmp;
	}

	/**
	 * 根据屏幕系数比例获取文字大小
	 * 
	 * @return
	 */
	private static float scalaFonts(int size) {
		// 暂未实现
		return size;
	}

	/**
	 * @return 返回指定笔和指定字符串的长度
	 */
	public static float getFontlength(Paint paint, String str) {
		return paint.measureText(str);
	}

	/**
	 * @return 返回指定笔的文字高度
	 */
	public static float getFontHeight(Paint paint) {
		FontMetrics fm = paint.getFontMetrics();
		return fm.descent - fm.ascent;
	}

	/**
	 * @return 返回指定笔离文字顶部的基准距离
	 */
	public static float getFontLeading(Paint paint) {
		FontMetrics fm = paint.getFontMetrics();
		return fm.leading - fm.ascent;
	}

}
